class sprite {
	public setup(p: any) {

	}
	public draw(p: any) {

	}
}
class xyz_sprite extends sprite {
	draw(p: any) {
		var a = 50;
		var b = 50 / 50;
		var o = a * 0.5;
		p.push(); p.translate(o, 0, 0); p.ambientMaterial(255, 0, 0); p.box(a, b, b); p.pop();
		p.push(); p.translate(0, o, 0); p.ambientMaterial(0, 255, 0); p.box(b, a, b); p.pop();
		p.push(); p.translate(0, 0, o); p.ambientMaterial(0, 0, 255); p.box(b, b, a); p.pop();
	}
}

class ground_sprite extends sprite{
    _img:any;
    _x:number;
    setup(p:any){
	this._img = p.createGraphics(50,50);
	this._img.background(200);
	this._img.stroke(153);
	this._img.line(0, 25, 50, 25);
	this._img.line(25, 0, 25, 50);
	this._img.line(0, 0, 50, 0);
	this._img.line(50, 0, 50, 50);
	this._img.line(50, 50, 50, 0);
	this._img.line(0, 0, 0, 50);
	this._x = 0;
    }
    draw(p:any){
	for(let i = -9; i < 10; i++){
	    for(let j = -9; j < 10; j++){
		p.push();
		p.translate(i * 10, j * 10, 0);
		p.texture(this._img);
		p.plane(10);
		p.pop();
	    }
	}
    }
}
class wallbox extends sprite {
    _data:any;
    constructor(data){
	this._data = data;
    }
    draw(p:any){
	
	this._data.map(d=>{
	    p.push();
	    if(d.Type == 2){
		p.translate(d.X, d.Y, 2);
		p.ambientMaterial(255,255,255);
		p.box(1, 1, 4);
		//p.translate(0-d.X, 0-d.Y, -2);
	    }else if(d.Type == 1){
		p.translate(d.X, d.Y, 1.5);
		p.ambientMaterial(255,153, 0);
		p.box(1, 1, 3);
		//p.translate(0-d.X, 0-d.Y, -1.5);
	
	    }
	    	p.pop();
	});

    }
}
class p3d {
	_doc: HTMLElement;
	_p: any;
	_render: any;
	_easycam: any;

	_testimg: any;
	_spriteArray: Array<sprite>;
	constructor(doc: HTMLElement) {
		this._doc = doc;
		this._spriteArray = [];
	}
	add(s: sprite) {
		this._spriteArray.push(s);
	}
	setup(that: p3d) {

		that._p.pixelDensity(1);

		let myCanvas = that._p.createCanvas(that._doc.clientWidth, that._doc.clientHeight, that._p.WEBGL);

		that._p.setAttributes('antialias', true);
		that._render = that._p._renderer;
		that._easycam = new Dw.EasyCam(that._render, { distance: 300 });
		that._doc.appendChild(that._p.canvas);

	
		that._spriteArray.map(x => x.setup(that._p));
	}
	windowResized(that: p3d) {
		that._p.resizeCanvas(that._doc.clientWidth, that._doc.clientHeight);
		that._easycam.setViewport([0, 0, that._doc.clientWidth, that._doc.clientHeight]);
	}
	draw(that: p3d) {
		that._p.perspective(60 * 3.14 / 180, that._doc.clientWidth / that._doc.clientHeight, 1, 5000);
		that._p.background(255);
		that._p.noStroke();
		// lights
		that._p.ambientLight(100);
		that._p.pointLight(255, 255, 255, 100, 0, 100);
		// objects
	
		that._spriteArray.map(x => x.draw(that._p));
	}
	init() {
		let that = this;
		let sketch = function (p: any): void {
			p.setup =
				() => {
					that.setup(that);
				};
			p.draw =
				() => {
					that.draw(that);
				};
			p.windowResized = () => {
				that.windowResized(that);
			};
		};

		this._p = new p5(sketch);

	}

}
